package com.ludi.advices;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
*@author:ludi
*@date:2020/8/20 11:33
*@description:测试spring-aop
*/
@Aspect
@Component
public class AnnotationAdvice {

    @Pointcut("execution(* com.ludi.service.impl.*ServiceImpl.*(..))")
    public void pointCut(){}
    @Before("AnnotationAdvice.pointCut()")
    public void before(){
        System.out.println("执行方法之前，打印日志");
    }

    @After("AnnotationAdvice.pointCut()")
    public void after(){
        System.out.println("执行方法之后，打印日志");
    }


    //后置通知(如果出现异常不会调用)
    @AfterReturning("AnnotationAdvice.pointCut()")
    public void afterReturning() {
        System.out.println("这是后置通知(方法不出现异常)");
    }

    //环绕通知
    @Around("AnnotationAdvice.pointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("这是环绕通知之前部分!!");
        Object object = point.proceed(); //调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return object;
    }

    //异常拦截通知
    @AfterThrowing(pointcut = "AnnotationAdvice.pointCut()",throwing = "e")
    public void afterException(Throwable e) {
        System.out.println("抛出异常时调用");
        System.out.println(e);
    }
}
